{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "26208929",
   "metadata": {},
   "source": [
    "## 1. APP 币本位合约行情-历史K线函数总览"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2189a8f",
   "metadata": {},
   "source": [
    "|方法|解释|\n",
    "|:---|:---|\n",
    "|get_history_candle|获取产品的历史K线数据|\n",
    "|get_history_candle_latest|获取产品指定数量的最新历史K线数据|\n",
    "|get_history_candle_by_date|获取产品指定日期的历史K线数据|\n",
    "|get_history_candle_latest_ts|获取历史K线数据中最新的毫秒时间戳|\n",
    "|update_history_candle|更新产品历史K线数据到指定时间|\n",
    "|candle_to_df|将Binance的candle转化为DataFrame|"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc013a12",
   "metadata": {},
   "source": [
    "## 2. 模型实例化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "39efbb71",
   "metadata": {},
   "outputs": [],
   "source": [
    "from binance_interface.app import BinanceCM\n",
    "from binance_interface.app.utils import eprint\n",
    "import paux.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f056f1c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 转发：需搭建转发服务器，可参考：https://github.com/pyted/binance_resender\n",
    "proxy_host = None\n",
    "key = 'xxxx'\n",
    "secret = 'xxxx'\n",
    "\n",
    "binanceCM = BinanceCM(\n",
    "    key=key,\n",
    "    secret=secret,\n",
    "    proxy_host=proxy_host,\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "market = binanceCM.market"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "443023f2",
   "metadata": {},
   "source": [
    "## 3. 获取产品的历史K线数据 get_history_candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f717edc1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67250240e+12, 1.65781000e+04, 1.65803000e+04, ...,\n",
      "        3.11300000e+03, 1.87770117e+01, 0.00000000e+00],\n",
      "       [1.67250246e+12, 1.65803000e+04, 1.65803000e+04, ...,\n",
      "        0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
      "       [1.67250252e+12, 1.65804000e+04, 1.65804000e+04, ...,\n",
      "        3.74900000e+03, 2.26110347e+01, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67258862e+12, 1.65399000e+04, 1.65430000e+04, ...,\n",
      "        3.07400000e+03, 1.85833263e+01, 0.00000000e+00],\n",
      "       [1.67258868e+12, 1.65430000e+04, 1.65446000e+04, ...,\n",
      "        1.50000000e+03, 9.06709593e+00, 0.00000000e+00],\n",
      "       [1.67258874e+12, 1.65453000e+04, 1.65460000e+04, ...,\n",
      "        9.73600000e+03, 5.88436566e+01, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle(\n",
    "    symbol='BTCUSD_PERP',\n",
    "    start='2023-01-01 00:00:00',\n",
    "    end='2023-01-01 23:59:00',\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd4a51b5",
   "metadata": {},
   "source": [
    "## 4. 获取产品指定数量的最新历史K线数据 get_history_candle_latest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d867cae5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.70615694e+12, 3.99759000e+04, 3.99759000e+04, ...,\n",
      "        2.52000000e+02, 6.30379800e-01, 0.00000000e+00],\n",
      "       [1.70615700e+12, 3.99759000e+04, 3.99795000e+04, ...,\n",
      "        1.67700000e+03, 4.19518756e+00, 0.00000000e+00],\n",
      "       [1.70615706e+12, 3.99795000e+04, 3.99840000e+04, ...,\n",
      "        8.52000000e+02, 2.13095785e+00, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.70619276e+12, 3.99779000e+04, 3.99920000e+04, ...,\n",
      "        2.94600000e+03, 7.36830830e+00, 0.00000000e+00],\n",
      "       [1.70619282e+12, 3.99768000e+04, 3.99817000e+04, ...,\n",
      "        1.71500000e+03, 4.28997026e+00, 0.00000000e+00],\n",
      "       [1.70619288e+12, 3.99817000e+04, 3.99872000e+04, ...,\n",
      "        6.32000000e+02, 1.58064849e+00, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle_latest(\n",
    "    symbol='BTCUSD_PERP',\n",
    "    length=600,\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7e70a5f",
   "metadata": {},
   "source": [
    "## 5. 获取产品指定日期的历史K线数据 get_history_candle_by_date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "73eafd53",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67250240e+12, 1.65781000e+04, 1.65803000e+04, ...,\n",
      "        3.11300000e+03, 1.87770117e+01, 0.00000000e+00],\n",
      "       [1.67250246e+12, 1.65803000e+04, 1.65803000e+04, ...,\n",
      "        0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
      "       [1.67250252e+12, 1.65804000e+04, 1.65804000e+04, ...,\n",
      "        3.74900000e+03, 2.26110347e+01, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67258862e+12, 1.65399000e+04, 1.65430000e+04, ...,\n",
      "        3.07400000e+03, 1.85833263e+01, 0.00000000e+00],\n",
      "       [1.67258868e+12, 1.65430000e+04, 1.65446000e+04, ...,\n",
      "        1.50000000e+03, 9.06709593e+00, 0.00000000e+00],\n",
      "       [1.67258874e+12, 1.65453000e+04, 1.65460000e+04, ...,\n",
      "        9.73600000e+03, 5.88436566e+01, 0.00000000e+00]]),\n",
      " 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "candle_result = market.get_history_candle_by_date(\n",
    "    symbol='BTCUSD_PERP',\n",
    "    date='2023-01-01',  # 默认时区为美国时区\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50e98847",
   "metadata": {},
   "source": [
    "## 6. 获取历史K线数据中最新的毫秒时间戳 get_history_candle_latest_ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "10cbd96c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200, 'data': 1706192880000.0, 'msg': ''}\n"
     ]
    }
   ],
   "source": [
    "ts_result = market.get_history_candle_latest_ts(\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(ts_result)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3afb76ae",
   "metadata": {},
   "source": [
    "## 7. 更新产品历史K线数据到指定时间 update_history_candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9a4f5360",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取candle1，待更新\n",
    "candle1 = market.get_history_candle(\n",
    "    symbol='BTCUSD_PERP',\n",
    "    start='2023-01-01 10:00:00',\n",
    "    end='2023-01-02 10:00:00',\n",
    "    bar='1m',\n",
    ")['data']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "31f27306",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'code': 200,\n",
      " 'data': array([[1.67258880e+12, 1.65461000e+04, 1.65495000e+04, ...,\n",
      "        1.85700000e+04, 1.12219613e+02, 0.00000000e+00],\n",
      "       [1.67258886e+12, 1.65495000e+04, 1.65540000e+04, ...,\n",
      "        1.72440000e+04, 1.04186162e+02, 0.00000000e+00],\n",
      "       [1.67258892e+12, 1.65543000e+04, 1.65700000e+04, ...,\n",
      "        2.67070000e+04, 1.61268588e+02, 0.00000000e+00],\n",
      "       ...,\n",
      "       [1.67267502e+12, 1.67247000e+04, 1.67247000e+04, ...,\n",
      "        5.07000000e+02, 3.03144451e+00, 0.00000000e+00],\n",
      "       [1.67267508e+12, 1.67246000e+04, 1.67246000e+04, ...,\n",
      "        0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
      "       [1.67267514e+12, 1.67223000e+04, 1.67234000e+04, ...,\n",
      "        7.54300000e+03, 4.51072737e+01, 0.00000000e+00]]),\n",
      " 'msg': ''}\n",
      "历史K线时间起点： 2023-01-02 00:00:00\n",
      "历史K线时间终点： 2023-01-02 23:59:00\n",
      "历史K线时间长度： 1440\n"
     ]
    }
   ],
   "source": [
    "# 更新candle1到指定日期时间(智能节约权重)\n",
    "candle_result = market.update_history_candle(\n",
    "    candle=candle1,  # 支持candle1为空\n",
    "    symbol='BTCUSD_PERP',\n",
    "    length=1440,  # 保留数量\n",
    "    end='2023-01-02 23:59:00',  # end默认为本地计算机时间戳\n",
    "    bar='1m',\n",
    ")\n",
    "eprint(candle_result)\n",
    "\n",
    "candle_start = paux.date.to_fmt(\n",
    "    candle_result['data'][0, 0],\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "\n",
    "candle_end = paux.date.to_fmt(\n",
    "    candle_result['data'][-1, 0],\n",
    "    timezone='Asia/Shanghai',\n",
    ")\n",
    "\n",
    "candle_length = candle_result['data'].shape[0]\n",
    "\n",
    "print('历史K线时间起点：', candle_start)\n",
    "print('历史K线时间终点：', candle_end)\n",
    "print('历史K线时间长度：', candle_length)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a9ef3671",
   "metadata": {},
   "source": [
    "## 8. 将Binance的candle转化为DataFrame candle_to_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8609def5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.67258880e+12, 1.65461000e+04, 1.65495000e+04, ...,\n",
       "        1.85700000e+04, 1.12219613e+02, 0.00000000e+00],\n",
       "       [1.67258886e+12, 1.65495000e+04, 1.65540000e+04, ...,\n",
       "        1.72440000e+04, 1.04186162e+02, 0.00000000e+00],\n",
       "       [1.67258892e+12, 1.65543000e+04, 1.65700000e+04, ...,\n",
       "        2.67070000e+04, 1.61268588e+02, 0.00000000e+00],\n",
       "       ...,\n",
       "       [1.67267502e+12, 1.67247000e+04, 1.67247000e+04, ...,\n",
       "        5.07000000e+02, 3.03144451e+00, 0.00000000e+00],\n",
       "       [1.67267508e+12, 1.67246000e+04, 1.67246000e+04, ...,\n",
       "        0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
       "       [1.67267514e+12, 1.67223000e+04, 1.67234000e+04, ...,\n",
       "        7.54300000e+03, 4.51072737e+01, 0.00000000e+00]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "candle = candle_result['data']\n",
    "candle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "763552db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>openTs</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>volume</th>\n",
       "      <th>closeTs</th>\n",
       "      <th>turnover</th>\n",
       "      <th>tradeNum</th>\n",
       "      <th>buyVolume</th>\n",
       "      <th>buyTurnover</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-01-02 00:00:00</td>\n",
       "      <td>16546.1</td>\n",
       "      <td>16549.5</td>\n",
       "      <td>16546.0</td>\n",
       "      <td>16549.4</td>\n",
       "      <td>18729.0</td>\n",
       "      <td>2023-01-02 00:00:59</td>\n",
       "      <td>113.180418</td>\n",
       "      <td>251.0</td>\n",
       "      <td>18570.0</td>\n",
       "      <td>112.219613</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-01-02 00:01:00</td>\n",
       "      <td>16549.5</td>\n",
       "      <td>16554.0</td>\n",
       "      <td>16549.4</td>\n",
       "      <td>16554.0</td>\n",
       "      <td>17332.0</td>\n",
       "      <td>2023-01-02 00:01:59</td>\n",
       "      <td>104.717900</td>\n",
       "      <td>215.0</td>\n",
       "      <td>17244.0</td>\n",
       "      <td>104.186162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-01-02 00:02:00</td>\n",
       "      <td>16554.3</td>\n",
       "      <td>16570.0</td>\n",
       "      <td>16554.3</td>\n",
       "      <td>16567.0</td>\n",
       "      <td>51092.0</td>\n",
       "      <td>2023-01-02 00:02:59</td>\n",
       "      <td>308.499808</td>\n",
       "      <td>873.0</td>\n",
       "      <td>26707.0</td>\n",
       "      <td>161.268588</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-01-02 00:03:00</td>\n",
       "      <td>16567.0</td>\n",
       "      <td>16567.0</td>\n",
       "      <td>16559.2</td>\n",
       "      <td>16559.3</td>\n",
       "      <td>41125.0</td>\n",
       "      <td>2023-01-02 00:03:59</td>\n",
       "      <td>248.335691</td>\n",
       "      <td>771.0</td>\n",
       "      <td>26800.0</td>\n",
       "      <td>161.837695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-01-02 00:04:00</td>\n",
       "      <td>16559.2</td>\n",
       "      <td>16559.3</td>\n",
       "      <td>16554.8</td>\n",
       "      <td>16554.8</td>\n",
       "      <td>13111.0</td>\n",
       "      <td>2023-01-02 00:04:59</td>\n",
       "      <td>79.184971</td>\n",
       "      <td>220.0</td>\n",
       "      <td>374.0</td>\n",
       "      <td>2.258660</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                openTs     open     high      low    close   volume  \\\n",
       "0  2023-01-02 00:00:00  16546.1  16549.5  16546.0  16549.4  18729.0   \n",
       "1  2023-01-02 00:01:00  16549.5  16554.0  16549.4  16554.0  17332.0   \n",
       "2  2023-01-02 00:02:00  16554.3  16570.0  16554.3  16567.0  51092.0   \n",
       "3  2023-01-02 00:03:00  16567.0  16567.0  16559.2  16559.3  41125.0   \n",
       "4  2023-01-02 00:04:00  16559.2  16559.3  16554.8  16554.8  13111.0   \n",
       "\n",
       "               closeTs    turnover  tradeNum  buyVolume  buyTurnover  \n",
       "0  2023-01-02 00:00:59  113.180418     251.0    18570.0   112.219613  \n",
       "1  2023-01-02 00:01:59  104.717900     215.0    17244.0   104.186162  \n",
       "2  2023-01-02 00:02:59  308.499808     873.0    26707.0   161.268588  \n",
       "3  2023-01-02 00:03:59  248.335691     771.0    26800.0   161.837695  \n",
       "4  2023-01-02 00:04:59   79.184971     220.0      374.0     2.258660  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = market.candle_to_df(candle)\n",
    "df.head()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
